package com.jsz.peini.controller.app;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jsz.peini.bean.order.OrderInfo;
import com.jsz.peini.bean.order.OrderInfoExample;
import com.jsz.peini.bean.order.OrderSeller;
import com.jsz.peini.bean.order.OrderSellerExample;
import com.jsz.peini.bean.order.OrderSellerExample.Criteria;
import com.jsz.peini.bean.seller.CompanyInfo;
import com.jsz.peini.bean.seller.CompanyInfoExample;
import com.jsz.peini.bean.seller.SellerInfo;
import com.jsz.peini.bean.seller.SellerInfoExample;
import com.jsz.peini.bean.seller.SellerJudge;
import com.jsz.peini.bean.seller.SellerJudgeExample;
import com.jsz.peini.bean.task.TaskInfo;
import com.jsz.peini.bean.user.UserInfo;
import com.jsz.peini.bean.user.UserSms;
import com.jsz.peini.bean.user.UserSmsExample;
import com.jsz.peini.common.util.DateTimeUtils;
import com.jsz.peini.common.util.MDUtil;
import com.jsz.peini.common.util.NumberFormatUtil;
import com.jsz.peini.common.util.PeiniUtils;
import com.jsz.peini.mapper.order.OrderInfoMapper;
import com.jsz.peini.mapper.order.OrderSellerMapper;
import com.jsz.peini.mapper.seller.CompanyInfoMapper;
import com.jsz.peini.mapper.seller.SellerInfoMapper;
import com.jsz.peini.mapper.seller.SellerJudgeMapper;
import com.jsz.peini.mapper.task.TaskInfoMapper;
import com.jsz.peini.mapper.user.UserInfoMapper;
import com.jsz.peini.mapper.user.UserSmsMapper;
import com.jsz.peini.sms.SmsSend;

/**
 * @author Han Guohong
 * @version 2017-2-18 下午8:35:18
 * 类说明
 */
@Controller
public class SellerOrderController {
	private static final Logger logger = LoggerFactory.getLogger(SellerOrderController.class);
	@Resource
	private SellerInfoMapper sellerInfoMapper;
	@Resource
	private OrderSellerMapper orderSellerMapper;
	@Resource
	private TaskInfoMapper taskInfoMapper;
	@Resource
	private OrderInfoMapper orderInfoMapper;
	@Resource
	private SellerJudgeMapper sellerJudgeMapper;
	@Resource
	private UserSmsMapper userSmsMapper;
	@Resource
	private CompanyInfoMapper companyInfoMapper;
	
	@Resource
	private UserInfoMapper userInfoMapper;

	/**
	 * 登录页面
	 * @author Han Guohong
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/seller/login")
	public String login(HttpServletRequest request,HttpServletResponse response){
		
		return "seller/login";
	}
	
	/**
	 * 登录操作
	 * @author Han Guohong
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/seller/doLogin",produces="text/plain;charset=UTF-8")
	public String loginDo(HttpServletRequest request,HttpServletResponse response){
		JSONObject object = new JSONObject();
		try {
			String accName = request.getParameter("accName");
			String accPass = request.getParameter("accPass");
			
			if(!accName.endsWith("@pn.com")){
				accName += "@pn.com";
			}
			
			if(StringUtils.isBlank(accName)){
				object.put("code", 0);
				object.put("msg", "用户名不能为空");
				return object.toString();
			}
			
			if(StringUtils.isBlank(accPass)){
				object.put("code", 0);
				object.put("msg", "密码不能为空");
				return object.toString();
			}
			
			SellerInfoExample example = new SellerInfoExample();
			example.createCriteria().andAccnameEqualTo(accName);
			List<SellerInfo> sellerInfos = sellerInfoMapper.selectByExample(example);
			if(sellerInfos.size() == 0){
				/*object.put("code", 0);
				object.put("msg", "用户名不存在");
				
				return object.toString();*/
				CompanyInfoExample companyExample = new CompanyInfoExample();
				companyExample.createCriteria().andAccnameEqualTo(accName);
				List<CompanyInfo> companyInfos = companyInfoMapper.selectByExampleWithBLOBs(companyExample);
				if(companyInfos.size() == 0){
					object.put("code", 0);
					object.put("msg", "用户名不存在");
					
					return object.toString();
				}else{
					CompanyInfo company = companyInfos.get(0);
					if(!company.getAccpass().equals(MDUtil.encodePwd(accPass))){
						object.put("code", 0);
						object.put("msg", "密码不正确");
						return object.toString();
					}
					
					String ids = "";
					example = new SellerInfoExample();
					example.createCriteria().andCompanyIdEqualTo(company.getId());
					List<SellerInfo> sellerList = sellerInfoMapper.selectByExampleWithBLOBs(example);
					for (SellerInfo sellerInfo : sellerList) {
						ids += sellerInfo.getId()+",";
					}
					ids = ids.substring(0, ids.length()-1);
					
					//request.getSession().setAttribute("sellerId", ids);
					//request.getSession().setAttribute("imgHost", PeiniUtils.IMGHOST);
					object.put("sellerId", ids);
					object.put("imgHost", PeiniUtils.IMGHOST);
					
					object.put("code", 1);
					object.put("msg", "success");
					return object.toString();
				}
				
			}else{
				SellerInfo sellerInfo = sellerInfos.get(0);
				if(!sellerInfo.getAccpass().equals(MDUtil.encodePwd(accPass))){
					object.put("code", 0);
					object.put("msg", "密码不正确");
					return object.toString();
				}
				
				//request.getSession().setAttribute("sellerId", sellerInfo.getId());
				//request.getSession().setAttribute("imgHost", PeiniUtils.IMGHOST);
				object.put("sellerId", sellerInfo.getId());
				object.put("imgHost", PeiniUtils.IMGHOST);
				
				object.put("code", 1);
				object.put("msg", "success");
				return object.toString();
			}
			
		} catch (Exception e) {
			object.put("code", 99);
			object.put("msg", "网络异常");
			logger.error(e.getMessage(),e);
		}
		return object.toString();
	}
	
	/**
	 * 订单列表
	 * @author Han Guohong
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/seller/orderList")
	public String orderList(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "sellerId") String sellerIds,
			@RequestParam(value = "status",defaultValue="100",required=false) Integer status,
			@RequestParam(value = "page",defaultValue="1",required=false) Integer page,
			@RequestParam(value = "rows",defaultValue="10",required=false) Integer rows
			){
		
		try {
			/*if(request.getSession().getAttribute("sellerId") == null){
				return "seller/login";
			}*/
			if(StringUtils.isBlank(sellerIds)){
				return "seller/login";
			}
			
			/*Long sellerId = Long.parseLong(request.getSession().getAttribute("sellerId").toString());
			SellerInfo sellerInfo = sellerInfoMapper.selectByPrimaryKey(sellerId.intValue());
			request.setAttribute("seller", sellerInfo);*/
			List<Long> idList = new ArrayList<Long>();
			String [] ids = sellerIds.split(",");
			for (int i = 0; i < ids.length; i++) {
				if(StringUtils.isNotBlank(ids[i])){
					idList.add(Long.parseLong(ids[i]));
				}
			}
			
			
			Page<?> p = PageHelper.startPage(page,rows);
			OrderSellerExample example = new OrderSellerExample();
			Criteria criteria = example.createCriteria();
			criteria.andSellerIdIn(idList);
			criteria.andOrderStatusNotEqualTo(0);
			criteria.andOrderStatusNotEqualTo(-1);
			//criteria.andSellerIdEqualTo(sellerId);
			if(status != 100){
				criteria.andOrderStatusEqualTo(status);
			}
			example.setOrderByClause("order_time desc");
			List<OrderSeller> list = orderSellerMapper.selectByExampleWithBLOBs(example);
			String path=PeiniUtils.IMGHOST;
			if(list.size()>0)
			{
				for (int i = 0; i < list.size(); i++) {
					OrderSeller seller = list.get(i);
					if(seller.getUserImageHead()==null)
					{
						seller.setUserImageHead("images/ic_nan.png");
					}else
					{
						seller.setUserImageHead(path+seller.getUserImageHead());
					}
					
					if(seller.getOtherImageHead()==null)
				    {
					   seller.setOtherImageHead("images/ic_nan.png");
				    }else
				    {
				    	seller.setOtherImageHead(PeiniUtils.IMGHOST+seller.getOtherImageHead());
				    }
					
					String orderCode = seller.getCodestr();
					if(StringUtils.isNotBlank(orderCode)){
						String [] codes = orderCode.split(";");
						for (String str : codes) {
							if(StringUtils.isNotBlank(str)){
								String [] aa = str.split(",");
								if(aa.length == 2){
									if(aa[0].equals(seller.getUserId())){
										seller.setUserOrderCode(aa[1]);
									}
									
									if(seller.getOtherId() != null && seller.getOtherId().equals(aa[0])){
										seller.setOtherUserOrderCode(aa[1]);
									}
								}else{
									if(seller.getOtherBy() != null){
										if(seller.getOtherBy() == 1){
											seller.setUserOrderCode(aa[0]);
										}else if(seller.getOtherBy() == 2){
											seller.setOtherUserOrderCode(aa[0]);
										}else{
											seller.setUserOrderCode(aa[0]);
										}
									}else{
										seller.setUserOrderCode(aa[0]);
									}
									
								}
							}
						}
					}
					
					list.set(i, seller);
				}
			}
			request.setAttribute("list", list);
			request.setAttribute("total", p.getTotal()/rows+1);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		return "seller/orderList";
	}
	
	/**
	 * 订单列表json
	 * @author Han Guohong
	 * @param request
	 * @param response
	 * @param status
	 * @param page
	 * @param rows
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/seller/orderListJson")
	public String orderListJson(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "sellerId") String sellerIds,
			@RequestParam(value = "status",defaultValue="100") Integer status,
			@RequestParam(value = "page",defaultValue="1") Integer page,
			@RequestParam(value = "rows",defaultValue="10") Integer rows
			){
		
		JSONObject object = new JSONObject();
		JSONArray array = new JSONArray();
		try {
			//Long sellerId = Long.parseLong(request.getSession().getAttribute("sellerId").toString());
			
			/*Long sellerId = Long.parseLong(request.getSession().getAttribute("sellerId").toString());
			SellerInfo sellerInfo = sellerInfoMapper.selectByPrimaryKey(sellerId.intValue());
			request.setAttribute("seller", sellerInfo);*/
			List<Long> idList = new ArrayList<Long>();
			String [] ids = sellerIds.split(",");
			for (int i = 0; i < ids.length; i++) {
				if(StringUtils.isNotBlank(ids[i]) && !"undefined".equals(ids[i])){
					idList.add(Long.parseLong(ids[i]));
				}
			}
			
			Page<?> p = PageHelper.startPage(page,rows);
			OrderSellerExample example = new OrderSellerExample();
			Criteria criteria = example.createCriteria();
			criteria.andOrderStatusNotEqualTo(0);
			criteria.andOrderStatusNotEqualTo(-1);
			//criteria.andSellerIdEqualTo(sellerId);
			criteria.andSellerIdIn(idList);
			if(status != 100){
				criteria.andOrderStatusEqualTo(status);
			}
			example.setOrderByClause("order_time desc");
			List<OrderSeller> list = orderSellerMapper.selectByExampleWithBLOBs(example);
			for (int i = 0; i < list.size(); i++) {
				OrderSeller orderSeller = list.get(i);
				JSONObject obj = JSONObject.fromObject(orderSeller);
				obj.put("orderTime", DateTimeUtils.formatDateTime2String(orderSeller.getOrderTime(), "yyyy-MM-dd HH:mm"));
//				obj.put("userImageHead", PeiniUtils.IMGHOST+orderSeller.getUserImageHead());
				if(orderSeller.getUserImageHead()==null)
				{
				
					obj.put("userImageHead", "images/ic_nan.png");
				}
				else
				{
					obj.put("userImageHead", PeiniUtils.IMGHOST+orderSeller.getUserImageHead());
				}
			    if(orderSeller.getOtherImageHead()==null)
			    {
			    		obj.put("otherImageHead", "images/ic_nan.png");
			    }
			    else
			    {
			    	obj.put("otherImageHead", PeiniUtils.IMGHOST+orderSeller.getOtherImageHead());
			    }
			    
			    String orderCode = orderSeller.getCodestr();
				if(StringUtils.isNotBlank(orderCode)){
					String [] codes = orderCode.split(";");
					for (String str : codes) {
						if(StringUtils.isNotBlank(str)){
							String [] aa = str.split(",");
							if(aa.length == 2){
								if(aa[0].equals(orderSeller.getUserId())){
									orderSeller.setUserOrderCode(aa[1]);
									obj.put("userOrderCode", aa[1]);
								}
								
								if(orderSeller.getOtherId() != null && orderSeller.getOtherId().equals(aa[0])){
									orderSeller.setOtherUserOrderCode(aa[1]);
									obj.put("otherUserOrderCode", aa[1]);
								}
							}else{
								if(orderSeller.getOtherBy() != null){
									if(orderSeller.getOtherBy() == 1){
										orderSeller.setUserOrderCode(aa[0]);
										obj.put("userOrderCode", aa[0]);
									}else if(orderSeller.getOtherBy() == 2){
										orderSeller.setOtherUserOrderCode(aa[0]);
										obj.put("otherUserOrderCode", aa[0]);
									}else{
										orderSeller.setUserOrderCode(aa[0]);
										obj.put("userOrderCode", aa[0]);
									}
								}else{
									obj.put("userOrderCode", aa[0]);
								}
							}
						}
					}
				}
			    
				obj.put("totalMoney", orderSeller.getTotalMoney() == null ? "" : NumberFormatUtil.format2RMB(((double)orderSeller.getTotalMoney())/100));
				obj.put("payMoney", orderSeller.getPayMoney() == null ? "" : NumberFormatUtil.format2RMB(((double)orderSeller.getPayMoney())/100));
				switch (orderSeller.getOrderStatus()) {
				case 0:
					obj.put("orderStatus", "待付款");
					break;
				case 1:
					obj.put("orderStatus", "待评价");
					break;
				case 2:
					obj.put("orderStatus", "已完成");
					break;
				case -1:
					obj.put("orderStatus", "已取消");
					break;
				default:
					break;
				}
				array.add(obj);
			}
			if(p.getTotal()/rows+1 < page){
				object.put("lists", new JSONArray());
			}else{
				object.put("lists", array);
			}
			
			//object.put("total", orderList.getTotal()/rows+1);
			object.put("code", 1);
			object.put("msg", "success");
		} catch (Exception e) {
			object.put("code", 99);
			object.put("msg", "网络异常");
			logger.error(e.getMessage(),e);
		}
//		logger.info(object.toString());
		return object.toString();
	}
	
	/**
	 * 关闭和开启店铺
	 * @author Han Guohong
	 * @param request
	 * @param response
	 * @param status
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/seller/setStatus")
	public String setStatus(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "id") Integer sellerId,
			@RequestParam(value = "status") Integer status
			){
		
		JSONObject object = new JSONObject();
		try {
			SellerInfo sellerInfo = sellerInfoMapper.selectByPrimaryKey(sellerId.intValue());
			sellerInfo.setSellerstatus(status);
			sellerInfoMapper.updateByPrimaryKey(sellerInfo);
			
			object.put("code", 1);
			object.put("msg", "success");
		} catch (Exception e) {
			object.put("code", 99);
			object.put("msg", "网络异常");
			logger.error(e.getMessage(),e);
		}
		return object.toString();
	}
	
	/**
	 * 订单详情
	 * @author Han Guohong
	 * @param request
	 * @param response
	 * @param orderId
	 * @return
	 */
	@RequestMapping("/seller/orderDetail")
	public String orderDetail(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "orderId") Long orderId
			){
		
		OrderSeller orderSeller = null;
		TaskInfo taskInfo = null;
		
		try {
			orderSeller = orderSellerMapper.selectByPrimaryKey(orderId);
			
			String path=PeiniUtils.IMGHOST;
			
			if(orderSeller.getUserImageHead()==null)
			{
				orderSeller.setUserImageHead("images/ic_nan.png");
			}else
			{
				orderSeller.setUserImageHead(path+orderSeller.getUserImageHead());
			}
			
			request.setAttribute("detail", orderSeller);
			
			if(orderSeller.getOrderType() == 1){
				Long taskId = orderSeller.getTaskId();
				taskInfo = taskInfoMapper.selectByPrimaryKey(taskId.intValue());
				request.setAttribute("taskInfo", taskInfo);
			}
			
			OrderInfoExample example = new OrderInfoExample();
			com.jsz.peini.bean.order.OrderInfoExample.Criteria criteria = example.createCriteria();
			if(orderSeller.getOrderType() == 1){
				criteria.andTaskInfoIdEqualTo(orderSeller.getTaskId().intValue());
			}else{
				criteria.andIdEqualTo(orderSeller.getTaskId());
			}
			List<OrderInfo> orderList = orderInfoMapper.selectByExample(example);
			for (OrderInfo orderInfo : orderList) {
				if(orderInfo.getUserId().equals(orderSeller.getUserId())){
					request.setAttribute("userOrder", orderInfo);
					
					if(orderSeller.getOrderStatus() == 2){
						SellerJudgeExample sellerJudgeExample = new SellerJudgeExample();
						sellerJudgeExample.createCriteria().andOrderIdEqualTo((long)orderInfo.getId());
						List<SellerJudge> judges = sellerJudgeMapper.selectByExample(sellerJudgeExample);
						if(judges.size() > 0){
							SellerJudge judge = judges.get(0);
							request.setAttribute("judge1", judge.getSellerScore());
						}
					}
				}
				if(orderSeller.getOrderType() == 1 && orderSeller.getOtherId() != null && orderSeller.getOtherId().equals(orderInfo.getUserId())){
					request.setAttribute("otherOrder", orderInfo);
					
					if(orderSeller.getOrderStatus() == 2){
						SellerJudgeExample sellerJudgeExample = new SellerJudgeExample();
						sellerJudgeExample.createCriteria().andOrderIdEqualTo((long)orderInfo.getId());
						List<SellerJudge> judges = sellerJudgeMapper.selectByExample(sellerJudgeExample);
						if(judges.size() > 0){
							SellerJudge judge = judges.get(0);
							request.setAttribute("judge2", judge.getSellerScore());
						}
					}
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		if(orderSeller.getOrderType() == 1){
			if(taskInfo != null && taskInfo.getTaskStatus() == 5){
				return "seller/orderCancelDetail";
			}else{
				return "seller/orderDetail";
			}
		}else{
			return "seller/orderDetail";
		}
	}
	
	/**
	 * 交易列表
	 * @author Han Guohong
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/seller/payList")
	public String payList(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "sellerId") String sellerIds,
			@RequestParam(value = "page",defaultValue="1") Integer page,
			@RequestParam(value = "rows",defaultValue="10") Integer rows
			){
		
		try {
			//Long sellerId = Long.parseLong(request.getSession().getAttribute("sellerId").toString());
			if(StringUtils.isBlank(sellerIds)){
				return "seller/login";
			}
			
			/*Long sellerId = Long.parseLong(request.getSession().getAttribute("sellerId").toString());
			SellerInfo sellerInfo = sellerInfoMapper.selectByPrimaryKey(sellerId.intValue());
			request.setAttribute("seller", sellerInfo);*/
			List<Integer> idList = new ArrayList<Integer>();
			String [] ids = sellerIds.split(",");
			for (int i = 0; i < ids.length; i++) {
				if(StringUtils.isNotBlank(ids[i])){
					idList.add(Integer.parseInt(ids[i]));
				}
			}
			
			Page<?> p = PageHelper.startPage(page,rows);
			/*OrderInfoExample example = new OrderInfoExample();
			List<Integer> statusList = new ArrayList<Integer>();
			statusList.add(1);
			statusList.add(2);
			example.createCriteria().andOrderStatusIn(statusList)
				//.andSellerInfoIdEqualTo(sellerId.intValue());
				.andSellerInfoIdIn(idList);
			
			example.setOrderByClause("order_time desc");
			List<OrderInfo> list = orderInfoMapper.selectByExample(example);*/
			
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("list", idList);
			List<LinkedHashMap<String, Object>> list = orderInfoMapper.getPayListBySeller(paramMap);
			
			request.setAttribute("list", list);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		return "seller/payList";
	}
	
	/**
	 * 交易明细json
	 * @author Han Guohong
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/seller/payListJson")
	public String payListJson(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "sellerId") String sellerIds,
			@RequestParam(value = "page",defaultValue="1") Integer page,
			@RequestParam(value = "rows",defaultValue="10") Integer rows
			){
		JSONObject object = new JSONObject();
		JSONArray array = new JSONArray();
		try {
			//Long sellerId = Long.parseLong(request.getSession().getAttribute("sellerId").toString());
			if(StringUtils.isBlank(sellerIds)){
				return "seller/login";
			}
			
			/*Long sellerId = Long.parseLong(request.getSession().getAttribute("sellerId").toString());
			SellerInfo sellerInfo = sellerInfoMapper.selectByPrimaryKey(sellerId.intValue());
			request.setAttribute("seller", sellerInfo);*/
			List<Integer> idList = new ArrayList<Integer>();
			String [] ids = sellerIds.split(",");
			for (int i = 0; i < ids.length; i++) {
				if(StringUtils.isNotBlank(ids[i])){
					idList.add(Integer.parseInt(ids[i]));
				}
			}
			
			Page<?> p = PageHelper.startPage(page,rows);
			/*OrderInfoExample example = new OrderInfoExample();
			List<Integer> statusList = new ArrayList<Integer>();
			statusList.add(1);
			statusList.add(2);
			example.createCriteria().andOrderStatusIn(statusList)
				//.andSellerInfoIdEqualTo(sellerId.intValue());
				.andSellerInfoIdIn(idList);
			
			example.setOrderByClause("order_time desc");
			List<OrderInfo> list = orderInfoMapper.selectByExample(example);*/
			
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("list", idList);
			List<LinkedHashMap<String, Object>> list = orderInfoMapper.getPayListBySeller(paramMap);
			
			for (LinkedHashMap<String, Object> orderInfo : list) {
				JSONObject obj = JSONObject.fromObject(orderInfo);
				Date orderTime = DateTimeUtils.formatString2Date(orderInfo.get("orderTime").toString(), "yyyy-MM-dd HH:mm:ss");
				obj.put("orderDate", DateTimeUtils.formatDateTime2String(orderTime, "MM-dd"));
				obj.put("orderTime", DateTimeUtils.formatDateTime2String(orderTime, "hh:mm"));
				obj.put("payType", orderInfo.get("payType").toString());
				obj.put("orderStatus", orderInfo.get("orderStatus").toString());
				obj.put("payMoney", NumberFormatUtil.format2RMB(Double.parseDouble(orderInfo.get("payMoney").toString())/100));
				array.add(obj);
			}
			if(p.getTotal()/rows+1 < page){
				object.put("lists", new JSONArray());
			}else{
				object.put("lists", array);
			}
			
			object.put("code", 1);
			object.put("msg", "success");
		} catch (Exception e) {
			object.put("code", 99);
			object.put("msg", "网络异常");
			logger.error(e.getMessage(),e);
		}
		//logger.info(object.toString());
		return object.toString();
	}
	
	@RequestMapping("/seller/smsList")
	public String smsList(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "sellerId") String sellerIds,
			@RequestParam(value = "page",defaultValue="1") Integer page,
			@RequestParam(value = "rows",defaultValue="10") Integer rows
			){
		
		try {
			//Long sellerId = Long.parseLong(request.getSession().getAttribute("sellerId").toString());
			if(StringUtils.isBlank(sellerIds)){
				return "seller/login";
			}
			
			/*Long sellerId = Long.parseLong(request.getSession().getAttribute("sellerId").toString());
			SellerInfo sellerInfo = sellerInfoMapper.selectByPrimaryKey(sellerId.intValue());
			request.setAttribute("seller", sellerInfo);*/
			List<String> idList = new ArrayList<String>();
			String [] ids = sellerIds.split(",");
			for (int i = 0; i < ids.length; i++) {
				if(StringUtils.isNotBlank(ids[i])){
					idList.add(ids[i]);
				}
			}
			
			Page<?> p = PageHelper.startPage(page,rows);
			UserSmsExample example = new UserSmsExample();
			List<Integer> statusList = new ArrayList<Integer>();
			statusList.add(21);
			statusList.add(22);
			statusList.add(23);
			example.createCriteria().andSmstypeIn(statusList)
				//.andToidEqualTo(sellerId.toString());
				.andToidIn(idList);
			example.setOrderByClause("sendTime desc");
			List<UserSms> list = userSmsMapper.selectByExampleWithBLOBs(example);
			request.setAttribute("list", list);
			
			idList.clear();
			for (UserSms userSms : list) {
				idList.add(userSms.getSmsid());
			}
			if(idList.size()!=0){
				userSmsMapper.cancelSmsRemind(idList);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		return "seller/smsList";
	}
	
	@ResponseBody
	@RequestMapping("/seller/smsListJson")
	public String smsListJson(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "sellerId") String sellerIds,
			@RequestParam(value = "page",defaultValue="1") Integer page,
			@RequestParam(value = "rows",defaultValue="10") Integer rows
			){
		
		JSONObject object = new JSONObject();
		JSONArray array = new JSONArray();
		try {
			//Long sellerId = Long.parseLong(request.getSession().getAttribute("sellerId").toString());
			if(StringUtils.isBlank(sellerIds)){
				return "seller/login";
			}
			
			/*Long sellerId = Long.parseLong(request.getSession().getAttribute("sellerId").toString());
			SellerInfo sellerInfo = sellerInfoMapper.selectByPrimaryKey(sellerId.intValue());
			request.setAttribute("seller", sellerInfo);*/
			List<String> idList = new ArrayList<String>();
			String [] ids = sellerIds.split(",");
			for (int i = 0; i < ids.length; i++) {
				if(StringUtils.isNotBlank(ids[i])){
					idList.add(ids[i]);
				}
			}
			
			Page<?> p = PageHelper.startPage(page,rows);
			UserSmsExample example = new UserSmsExample();
			List<Integer> statusList = new ArrayList<Integer>();
			statusList.add(21);
			statusList.add(22);
			statusList.add(23);
			example.createCriteria().andSmstypeIn(statusList)
					//.andToidEqualTo(sellerId.toString());
					.andToidIn(idList);
			
			example.setOrderByClause("sendTime desc");
			List<UserSms> list = userSmsMapper.selectByExampleWithBLOBs(example);
			
			idList.clear();
			
			for (UserSms sms : list) {
				idList.add(sms.getSmsid());
				
				JSONObject obj = JSONObject.fromObject(sms);
				obj.put("sendTime", DateTimeUtils.formatDateTime2String(sms.getSendtime(), "MM-dd hh:mm"));
				array.add(obj);
			}
			if(p.getTotal()/rows+1 < page){
				object.put("lists", new JSONArray());
			}else{
				object.put("lists", array);
			}
			
			
			if(idList.size()!=0){
				userSmsMapper.cancelSmsRemind(idList);
			}
			
			object.put("code", 1);
			object.put("msg", "success");
		} catch (Exception e) {
			object.put("code", 99);
			object.put("msg", "网络异常");
			logger.error(e.getMessage(),e);
		}
		//logger.info(object.toString());
		return object.toString();
	}
	
	@RequestMapping("/seller/shopList")
	public String shopList(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "sellerId") String sellerIds
			){
		
		try {
			if(StringUtils.isBlank(sellerIds)){
				return "seller/login";
			}
			
			List<Integer> idList = new ArrayList<Integer>();
			String [] ids = sellerIds.split(",");
			for (int i = 0; i < ids.length; i++) {
				if(StringUtils.isNotBlank(ids[i])){
					idList.add(Integer.parseInt(ids[i]));
				}
			}
			
			SellerInfoExample example = new SellerInfoExample();
			example.createCriteria().andIdIn(idList);
			List<SellerInfo> list = sellerInfoMapper.selectByExampleWithBLOBs(example);
			request.setAttribute("list", list);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		return "seller/shopList";
	}
}
